package com.hyper_yang.javase.collections;

import java.util.ArrayList;
import java.util.Iterator;

public class TryIterator {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        Iterator<String> iterator = list.iterator(); // ctrl + b 跳转: list内部类
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
        /*
            public Iterator<E> iterator() {
                return new Itr();
            }

            final void checkForComodification() {
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException(); // 不匹配说明在遍历时改变了集合状态，会存在并发修改异常
            }

            private class Itr implements Iterator<E> {
                int cursor;       // index of next element to return
                int lastRet = -1; // index of last element returned; -1 if no such
                int expectedModCount = modCount; // 获取 modCount 修改次数

                // prevent creating a synthetic constructor
                Itr() {}

                public boolean hasNext() {
                    return cursor != size;
                }

                @SuppressWarnings("unchecked")
                public E next() {
                    checkForComodification();
                    int i = cursor;
                    if (i >= size)
                        throw new NoSuchElementException();
                    Object[] elementData = ArrayList.this.elementData;
                    if (i >= elementData.length)
                        throw new ConcurrentModificationException();
                    cursor = i + 1;
                    return (E) elementData[lastRet = i];
                }
        */

    }
}
